// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <melon/benchmark.h>
#include <melon/init/init.h>
#include <pollux/functions/macros.h>
#include <pollux/functions/registerer.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>

using namespace kumo::pollux;
using namespace kumo::pollux::exec;

namespace {

template <typename T>
struct WidthBucketFunction {
  POLLUX_DEFINE_FUNCTION_TYPES(T);

  MELON_ALWAYS_INLINE bool call(
      int64_t& result,
      double operand,
      double bound1,
      double bound2,
      int64_t bucketCount) {
    double lower = std::min(bound1, bound2);
    double upper = std::max(bound1, bound2);

    if (operand < lower) {
      result = 0;
    } else if (operand > upper) {
      result = bucketCount + 1;
    } else {
      result =
          (int64_t)((double)bucketCount * (operand - lower) / (upper - lower) +
                    1);
    }

    if (bound1 > bound2) {
      result = bucketCount - result + 1;
    }
    return true;
  }
};

class WidthBucketBenchmark : public functions::test::FunctionBenchmarkBase {
 public:
  WidthBucketBenchmark() : FunctionBenchmarkBase() {
    functions::prestosql::registerArithmeticFunctions();

    register_function<
        WidthBucketFunction,
        int64_t,
        double,
        double,
        double,
        int64_t>({"width_bucket_no_check"});
  }

  void run(const std::string& functionName) {
    melon::BenchmarkSuspender suspender;
    vector_size_t size = 1'000;
    auto operandVector = vectorMaker_.flat_vector<double>(
        size, [](auto row) { return row + 3.14; });
    auto bound1Vector =
        vectorMaker_.flat_vector<double>(size, [](auto row) { return row; });
    auto bound2Vector =
        vectorMaker_.flat_vector<double>(size, [](auto row) { return row + 4; });
    auto bucketCountVector =
        BaseVector::create_constant(INTEGER(), 3, size, execCtx_.pool());

    auto row_vector = vectorMaker_.row_vector(
        {operandVector, bound1Vector, bound2Vector, bucketCountVector});
    auto exprSet = compileExpression(
        fmt::format("{}(c0, c1, c2, c3)", functionName), row_vector->type());
    suspender.dismiss();

    doRun(exprSet, row_vector);
  }

  void doRun(ExprSet& exprSet, const RowVectorPtr& row_vector) {
    int cnt = 0;
    for (auto i = 0; i < 100; i++) {
      cnt += evaluate(exprSet, row_vector)->size();
    }
    melon::doNotOptimizeAway(cnt);
  }
};

BENCHMARK(widthBucketNoCheck) {
  WidthBucketBenchmark benchmark;
  benchmark.run("width_bucket_no_check");
}

BENCHMARK_RELATIVE(widthBucket) {
  WidthBucketBenchmark benchmark;
  benchmark.run("width_bucket");
}

} // namespace

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};

  melon::runBenchmarks();
  return 0;
}
